//
//  SetGestureLockViewController.m
//  app
//
//  Created by 余钦 on 16/5/4.
//
//

#import "SetGestureLockViewController.h"
#import "GestureLockView.h"
#import "WYCMePhotoModel.h"
#import "configuration.h"
#import "WYCLoginViewController.h"

#define tipLabelFontSize 20

#define MarginY 10

#define TextColor [UIColor blueColor]

@interface SetGestureLockViewController ()<GestureLockViewDelegate>


@property(nonatomic, weak)UIImageView *lockViewShotView;
@property(nonatomic, weak)UILabel *tipLabel;
@property(nonatomic, weak)GestureLockView *gestureLockView;
@property(nonatomic, weak)UIButton *resetButton;
@property(nonatomic, copy)NSString *firstLockPath;
@property(nonatomic, copy)NSString *secondLockPath;
/** 模型*/
@property(nonatomic,strong)WYCMePhotoModel * MePhotoModel;
/** 头像*/
@property(nonatomic,strong)UIImageView * photoImageView;
@end

@implementation SetGestureLockViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self SetupSubviews];
    self.title = @"设置手势密码";
    
    [self requster];
}
- (void)requster{
    
//    NSString * str = [NSString stringWithFormat:@"%@token=%@",userPortraitDetail,[WYCAccountTool unarchivetoken]];
//    [HttpTool POST:str params:nil success:^(NSURLSessionDataTask *task, id responseObject) {
//        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
//            NSDictionary * dict = responseObject[@"result"];
//            self.MePhotoModel = [WYCMePhotoModel mj_objectWithKeyValues:dict];
//            if (self.MePhotoModel.imgUrl.length == 0){
//                self.photoImageView.image = [UIImage imageNamed:@"melogo"];
//
//            }else{
//                [self.photoImageView sd_setImageWithURL:[NSURL URLWithString:self.MePhotoModel.imgUrl]];
//            }
//        }else{
//            [self showToast:msgStr];
//
//        }
//
//    } fail:^(NSURLSessionDataTask *task, NSError *error) {
//        [self showToast:Failed];
//    }];
}
- (void)SetupSubviews{
    
    
    UIImageView * cellImage = [[UIImageView alloc] init];
    
    cellImage.frame = CGRectMake(ScreenWidth / 2 - 120 * px,kDevice_Is_iPhoneX?88:64 + 80 * px, 240 * px, 240 * px);
    cellImage.layer.cornerRadius = 120 * px;
    cellImage.layer.masksToBounds = YES;
    
cellImage.image = [UIImage imageNamed:@"图层12"];

    [self.view  addSubview:cellImage];
    self.photoImageView = cellImage;
    
    
    
    CGFloat mainViewW = self.view.frame.size.width;
    CGFloat mainViewH = self.view.frame.size.height;
    
    CGFloat smallLockViewW = 100.0f;
    CGFloat smallLockViewH = 100.0f;
    CGFloat smallLockViewX = (mainViewW - smallLockViewW)/2.0f;
    CGFloat smallLockViewY = kDevice_Is_iPhoneX ? 88+20.0f:64+20;
    UIImageView *lockViewShotView = [[UIImageView alloc] init];
    lockViewShotView.frame = CGRectMake(smallLockViewX, smallLockViewY, smallLockViewW, smallLockViewH);
    [self.view addSubview:lockViewShotView];
    self.lockViewShotView = lockViewShotView;
    
    CGFloat tipLabelH = 32.0f;
    UILabel *tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.lockViewShotView.frame) + MarginY, mainViewW, tipLabelH)];
    tipLabel.backgroundColor = [UIColor clearColor];
    tipLabel.text = @"请输入您的手势密码";
    tipLabel.textColor = wycMain;
    tipLabel.font = [UIFont systemFontOfSize:tipLabelFontSize];
    tipLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:tipLabel];
    self.tipLabel = tipLabel;
    
    CGFloat lockViewW = self.view.frame.size.width;
    GestureLockView *lockView = [[GestureLockView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.tipLabel.frame) + MarginY, lockViewW, lockViewW)];
    lockView.delegate = self;
    [self.view addSubview:lockView];
    self.gestureLockView = lockView;
    NSLog(@"nage%@",self.myflag);
    
        UIButton * loginButton = [[UIButton alloc] init];
        loginButton.frame = CGRectMake(120 * px, SCREEN_HEIGHT-70, ScreenWidth - 240 * px, 150 * px);
        loginButton.backgroundColor = [UIColor whiteColor];
        loginButton.layer.cornerRadius = 10;
        [loginButton setTitle:@"忘记手势密码" forState:UIControlStateNormal];
        loginButton.titleLabel.font = [UIFont systemFontOfSize:72 * px];
        [loginButton setTitleColor:wycMain forState:UIControlStateNormal];
        [loginButton addTarget:self action:@selector(pressforget) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:loginButton];
    if ([self.myflag isEqualToString:@"456"]) {
        loginButton.hidden =YES;
    }
    
}
//忘记手势密码
-(void)pressforget
{
    NSUserDefaults *useDef = [NSUserDefaults standardUserDefaults];
    
    // 保存用户数据
    [useDef removeObjectForKey:@"first"];
    
    [useDef synchronize];
    
    [WYCAccountTool clearAccount];
    WYCLoginViewController * login = [[WYCLoginViewController alloc] init];
    AppDelegate *myDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    myDelegate.window.rootViewController = login;
    
}
- (void)ClickCancelButton{
   
    [self.navigationController popViewControllerAnimated:NO];
}

- (void)ClickResetButton{
    self.firstLockPath = @"";
    self.tipLabel.textColor = wycMain;
    self.tipLabel.text = @"请输入您的手势密码";
    //    [self GetLockViewShot];
}

#pragma mark ----- GestureLockView delegate
- (void)lockView:(GestureLockView *)lockView BeganTouch:(NSSet *)touchs
{
    self.tipLabel.textColor = wycMain;
    self.tipLabel.text = @"请再次输入您的手势密码";
}

- (void)lockView:(GestureLockView *)lockView didFinishPath:(NSString *)path shotImage:(UIImage *)shotImage
{
    
    if (self.string == YES) {
        if (path.length < 4) {
            self.tipLabel.textColor = [UIColor redColor];
            self.tipLabel.text = @"请连接至少4个点";
            return ;
        }
        
        if (self.firstLockPath.length) {
            if ([path isEqualToString:self.firstLockPath]) {
                self.tipLabel.textColor = wycMain;
                self.tipLabel.text = @"手势密码设置成功";
                [self SaveLockPath:path];
               [NSThread sleepForTimeInterval:1.2f];
                
                
                [self.navigationController popViewControllerAnimated:YES];
                if (_noticBlock) {
                    _noticBlock(NO);
                    
                }
            }else{
                self.tipLabel.textColor = [UIColor redColor];;
                self.tipLabel.text = @"两次密码输入不一致";
                self.resetButton.hidden = NO;
            }
        }else{
            self.firstLockPath = [path copy];
            //        self.lockViewShotView.image  = shotImage;
        }
    } else {
        NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
        NSString * userlogin =[userDef objectForKey:@"LockPath"];
        if ([path isEqualToString:userlogin]) {
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"yes" object:nil];
            
            
        } else {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"no" object:nil];
            self.tipLabel.text = @"手势密码不正确，重新输入";
            [SVProgressHUD showErrorWithStatus:@"手势密码不正确，重新输入"];
        }
    }
}

- (void)SaveLockPath:(NSString *)path{
    NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
    [userDef setObject:path forKey:@"LockPath"];
    [userDef synchronize];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
}
- (void)backClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}
@end

